package com.sec.springcloud.Controller;

import com.sec.Service.scService;
import com.sec.springcloud.pojo.scinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class scController {


    @Autowired
    scService service;
    @Autowired
    DiscoveryClient client;

    //添加
    @PostMapping("/add/{name}")
    public boolean add(@PathVariable String name)
    {
        return service.save(new scinfo().setName(name));
    }

    //查询单个

    @GetMapping("/queryId/{id}")
    public scinfo queryId(@PathVariable Long id)
    {
        return service.getById(id);
    }


    @GetMapping("/query")
    public List query()
    {
        return service.list();
    }


    @Value(value = "${spring.application.name}")
    String serviceId;
    @GetMapping("/discovery")
    public Object discovery(){
        //获得微服务列表清单
        List<String> list = client.getServices();
        System.out.println("client.getServices()==>"+list);
        //得到一个具体的微服务！
        List<ServiceInstance> serviceInstanceList =
                client.getInstances(serviceId);
        for (ServiceInstance serviceInstance : serviceInstanceList) {
            System.out.println(
                    serviceInstance.getServiceId()+"\t"+
                            serviceInstance.getHost()+"\t"+
                            serviceInstance.getPort()+"\t"+
                            serviceInstance.getUri()
            );
        }
        return this.client;
    }





}
